package com.sy;


import com.sy.config.MainConfig;
import com.sy.controller.WelcomeController;
import com.sy.demo.Hello;
import com.sy.demo.HelloFactory;
import com.sy.demo.LogService;
import com.sy.entity.User;
import com.sy.service.WelcomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.FileSystemXmlApplicationContext;


@Configuration  // 包含 @Component 所以也会被当做Bean放到容器中
@ComponentScan("com.sy")
public class Entrance {




	/**
	 * 使用xml文件配置的方式
	 * @param args
	 */
	public static void main01(String[] args) {
		System.out.println("Hello Spring");
		// 1、配置文件路径
		String xmlPath = "//Users/syisme/MyJava/source/spring-framework-5.2.0.RELEASE/SpringDemo/src/main/resources/spring/spring-config2.xml";
		// 2、解析配置文件路径获取容器
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
		HelloFactory bean = applicationContext.getBean(HelloFactory.class);
		System.out.println(bean);
		// 3、获取容器中的实例化对象并强制转换
//		WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService");
//		welcomeService.sayHello("Hello Xml");

	}


	/**
	 * 使用注解方式
	 * @param args
	 */
	public static void main(String[] args) {

		AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MainConfig.class);
		System.out.println(app.getBean(Hello.class));
		app.getBean(LogService.class).sayHello("sy");
//		String[] beanDefinitionNames = app.getBeanDefinitionNames();
//		for (String beanName: beanDefinitionNames) {
//			System.out.println(beanName);
//		}
//
//		WelcomeController welcomeController = (WelcomeController) app.getBean("welcomeController");
//		welcomeController.testRequest();
//
//		User userPost = (User) app.getBean("UserPost");
//		System.out.println(userPost);
//		System.out.println("当前容器中的所有组件");
//		for (String beanDefinitionName : app.getBeanDefinitionNames()) {
//			System.out.println(beanDefinitionName);
//		}
	}
}
